package com.yljk.servicemanager.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yilijk.base.utils.ALog;
import com.yljk.servicemanager.R;

public class CustomeDialog extends Dialog {
    public CustomeDialog(@NonNull Context context) {
        super(context);
    }

    public CustomeDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected CustomeDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder{
        private Context context;
        public Builder(Context context) {
            this.context = context;
        }
        public CustomeDialog create() {
           try {
               LayoutInflater inflater = (LayoutInflater) context
                       .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
               // instantiate the dialog with the custom Theme
               final CustomeDialog dialog = new CustomeDialog(context, R.style.Dialog);
               View layout = inflater.inflate(R.layout.loading_layout, null);
               //获取背景，并将其强转成AnimationDrawable
               ImageView loadingImageView = (ImageView) layout.findViewById(R.id.loadingImageView);
               AnimationDrawable animationDrawable = (AnimationDrawable) loadingImageView.getBackground();
               //判断是否在运行
               if(!animationDrawable.isRunning()){
                   //开启帧动画
                   animationDrawable.start();
               }
               dialog.setCancelable(false);
               dialog.setContentView(layout);
               Window window = dialog.getWindow();
               window.setBackgroundDrawableResource(android.R.color.transparent);
               //重新设置
               WindowManager.LayoutParams lp = window.getAttributes();
               lp.dimAmount = 0;
               window.setAttributes(lp);
               return dialog;
           }catch (Exception e){
               ALog.e("CustomeDialog", "create: "+e.getMessage().toString() );
               return null;
           }
        }

    }


}
